package test04.sync.test05.t2;

/**
 * 定义一个线程类，模拟电影院售票
 *
 * @Author lzp
 * @Date 2024-03-21
 */
public class WindowThread extends Thread{
    private Film film ;

    public WindowThread(Film film,String name) {
        super(name);
        this.film = film ;
    }

    private String loke="lock";

    @Override
    public void run() {

        while (film.getCount() >= 1){
            synchronized (loke){
                if(film.getCount()>0){
                    System.out.println(Thread.currentThread().getName()+"出售了第"+film.getCount()+"张票");
                    film.setCount(film.getCount()-1);
                }

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }

        }
    }
}
